import { useEffect, useState } from 'react'

export const useCache = (key,value,mode="get") => {
    let cacheKey = "DataCache"
    const [cache, setCache] = useState({})
    useEffect(()=>{
        // 初始化
        setCache(localStorage.getItem(cacheKey))
        if(mode === "set"){
            cache[key] = value
            setCache(cache)
            localStorage.setItem(cacheKey, JSON.stringify(cache))
        }else if(mode === "delete"){
            delete cache[key]
            setCache(cache)
            localStorage.setItem(cacheKey, JSON.stringify(cache))
        }
    },[])
    return cache[key] || ""

}
// 获取登录状态
export const useLoginStatus = () =>{
    let status = useCache("loginStatus")
    if(status == true){
        return true
    }else{
        return false
    }
}
// 设置登陆信息
export const useLoginUser = (userMessage) =>{
    useCache("loginUser",userMessage,"set")
    useCache("loginStatus",true,"set")
    return true
}
// 滑动到顶部
export const useScrollToTop = (el) =>{
    el.scrollTo(0,0)
}